package cn.wx.scholar;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import com.csvreader.CsvReader;

import cn.wx.common.utils.JSONUtils;
public class ReadCsvPatentsExpert {

	
	
	
	
	public static void main(String[] args) {
		
		final String filePath = "D:\\my_read_csv\\result.csv";
		
		final String expertfilePath = "D:\\my_read_csv\\expert_all.csv";
		
		Map<String,String> map = new HashMap<>();
		
		try {
			
			
			File file1 = new File(expertfilePath);
	        FileInputStream fileInputStream1 = new FileInputStream(file1);  
	        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
			CsvReader csvReader1 = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
			
			// 读表头
			csvReader1.readHeaders();
            int ii = 0;
            while (csvReader1.readRecord()){
                // 读一整行
            	String line = csvReader1.getRawRecord();
				System.out.println("读取专家" + (ii++) +"行");
				
				String eId = csvReader1.get(0);
				String eName = csvReader1.get(1);
				
				map.put(eName.toLowerCase(), eId);
            }
			
			
			
			
			File file = new File(filePath);
	        FileInputStream fileInputStream = new FileInputStream(file);  
	        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			CsvReader csvReader = new CsvReader(bufferedInputStream,Charset.forName("utf-8"));
			
			File of = new File("D:\\my_read_csv\\result_2.csv");
			FileOutputStream ofs = new FileOutputStream(of,true);
			OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
			BufferedWriter output = new BufferedWriter(ofw);

			// 读表头
            csvReader.readHeaders();
            int i = 0;
            while (csvReader.readRecord()){
                // 读一整行
            	String line = csvReader.getRawRecord();
				System.out.println("读取" + (i++) +"行");
				if(StringUtils.isBlank(line)) {
					continue;
				}

				/**
				 * 解析csv
				 */
				String patentsId = csvReader.get(0);
				String authors = csvReader.get(2);
				StringBuffer sb = null;
				
				if(StringUtils.isNotBlank(authors)) {
					
					String[] aes = authors.split("\\|");
					
					for(String s:aes) {
						if(StringUtils.isBlank(s)) {
							continue;
						}
						
						String an = s.split("~")[0];
						String eId_ = map.get(an.toLowerCase());
						if(StringUtils.isBlank(eId_)) {
							continue;
						}
						sb = new StringBuffer();
						sb.append(patentsId).append(",").append(JSONUtils.c(eId_));
						
						output.write(sb.toString());
						output.newLine();
					}
					output.flush();
				}
				
            }
			
            output.flush();
            output.close();
            output = null;
            
            /**
			 * 关闭读入流
			 */
			csvReader.close();
			csvReader = null;
            
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
